package com.iot.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.iot.core.JsonHelper;
import com.iot.core.LResult;
import com.iot.core.PageData;
import com.iot.pojo.MTline_emp_set;
import com.iot.service.Tline_emp_setService;
import com.iot.util.CenterPointAndZoom;
import com.iot.util.Common;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/line/emp/")
public class TLine_emp_setController extends BaseController {
    @Autowired
    private Tline_emp_setService service;

    @ResponseBody
    @RequestMapping("save")
    public String save(@RequestParam String bean) {
        try {

            MTline_emp_set entity = JsonHelper.toBean(bean, MTline_emp_set.class);
            if (entity.getLINE_EMP_SET_ID() > 0) {
                service.update(entity);
            } else {
                service.insert(entity);

            }
            return LResult.Success(entity.getLINE_EMP_SET_ID()).toJson();
        } catch (Exception ex) {
            return LResult.Error(ex.getMessage()).toJson();
        }
    }

    @Transactional(rollbackFor = Throwable.class)
    @ResponseBody
    @RequestMapping("saveData")
    public String saveData(@RequestParam String bean, @RequestParam String idsStr) {
        try {

            MTline_emp_set entity = JsonHelper.toBean(bean, MTline_emp_set.class);
            String[] ar= idsStr.split(",");
            int[] ids=new int[ar.length];
            if (ar.length>0) {
                for (int i = 0; i <ar.length ; i++) {
                    ids[i]= Common.toInt(ar[i]);
                }
            }


            int id = service.saveData(entity, ids);

            return LResult.Success(id).toJson();
        } catch (Exception ex) {
            return LResult.Error(ex.getMessage()).toJson();
        }
    }

    @ResponseBody
    @RequestMapping("get")
    public String getByid(@RequestParam Integer id) {
        try {
            //  String menu = request.getParameter("menu");
            MTline_emp_set bean = service.single(id);
            return LResult.Success(bean).toJson();
        } catch (Exception ex) {
            return LResult.Error(ex.getMessage()).toJson();
        }
    }

    @ResponseBody
    @RequestMapping("getLineSetById")
    public String getLineSetById(@RequestParam Integer id) {
        try {
            //  String menu = request.getParameter("menu");
            Map<String, Object> bean = service.getLineSetById(id);
            return LResult.Success(bean).toJson();
        } catch (Exception ex) {
            return LResult.Error(ex.getMessage()).toJson();
        }
    }

    @ResponseBody
    @RequestMapping("del")
    public String deleteByid(@RequestParam Integer id) {
        try {
            //  String menu = request.getParameter("menu");


            service.deleteById(id);



            return LResult.Success().toJson();
        } catch (Exception ex) {
            return LResult.Error(ex.getMessage()).toJson();
        }
    }

    @ResponseBody
    @RequestMapping("query")
    public String queryData() {

        try {
            PageData map = getPageData();
            if (!map.containsKey("appId")) {
                map.put("appId", 0);
            }
            if (!map.containsKey("linename")) {
                map.put("linename", "");
            }

            if (!map.containsKey("lineId")) {
                map.put("lineId", 0);
            }
            if (!map.containsKey("siteId")) {
                map.put("siteId", 0);
            }
            if (!map.containsKey("regionId")) {
                map.put("regionId", 0);
            }
            if (!map.containsKey("empId")) {
                map.put("empId", 0);
            }
            PageHelper.startPage(Common.toInt(map.get("page")), Common.toInt(map.get("limit")));
            List<Map<String, Object>> list = service.fetchByPage(map);
            PageInfo<Map<String, Object>> pageInfo = new PageInfo<>(list);
            return LResult.Success(pageInfo.getTotal(), pageInfo.getList()).toJson();
        } catch (Exception ex) {
            return  LResult.Error("数据错误").toJson();
        }
    }

    @ResponseBody
    @RequestMapping("getDeviceByEmpId")
    public String getDeviceByEmpId() {

        try {
            PageData map = getPageData();
            if (!map.containsKey("empId")) {
                throw new Exception("缺少必要参数！");
            }
            if (!map.containsKey("statusId")) {
                map.put("statusId", 0);
            }
//            if (!map.containsKey("lat")||!map.containsKey("log")) {
//                throw new Exception("缺少必要的位置信息！");
//            }
            if (!map.containsKey("lat")) {
                map.put("lat", "");
            }
            if (!map.containsKey("log")) {
                map.put("log", "");
            }
            List<Map<String, Object>> list = service.getDeviceByEmpId(map);
            Integer count = list.size();
            return LResult.Success(count,list).toJson();
        } catch (Exception ex) {
            return  LResult.Error("数据错误").toJson();
        }

    }

    @ResponseBody
    @RequestMapping("getCPZ")
    public String getCPZ() {

        try {
            PageData map = getPageData();
            if (!map.containsKey("appId")) {
                throw new Exception("缺少必要参数！");
            }
            if (!map.containsKey("empId")) {
                throw new Exception("缺少必要参数！");
            }
            if (!map.containsKey("statusId")) {
                map.put("statusId", 0);
            }
//            if (!map.containsKey("lat")||!map.containsKey("log")) {
//                throw new Exception("缺少必要的位置信息！");
//            }
            if (!map.containsKey("lat")) {
                map.put("lat", "");
            }
            if (!map.containsKey("log")) {
                map.put("log", "");
            }
            CenterPointAndZoom resmap = service.getCPZ(map);
            return LResult.Success(resmap).toJson();
        } catch (Exception ex) {
            return LResult.Error(ex.getMessage()).toJson();
        }

    }

    @ResponseBody
    @RequestMapping("getDeviceByEmpSetId")
    public String getDeviceByEmpSetId(int empId) {
        try {
            //  String menu = request.getParameter("menu");
            List<Map<String, Object>> list = service.getDeviceByEmpSetId(empId);
            return LResult.Success(list).toJson();
        } catch (Exception ex) {
            return LResult.Error(ex.getMessage()).toJson();
        }
    }


    @ResponseBody
    @RequestMapping("initList")
    public String initList(int appId) {
        try {
            Map<String, Object> map = service.initList(appId);
            return LResult.Success(map).toJson();
        } catch (Exception ex) {
            return LResult.Error(ex.getMessage()).toJson();
        }
    }

    @ResponseBody
    @RequestMapping("initEdit")
    public String initEdit(int appId) {
        try {
            Map<String, Object> map = service.initEdit(appId);
            return LResult.Success(map).toJson();
        } catch (Exception ex) {
            return LResult.Error(ex.getMessage()).toJson();
        }
    }


    @ResponseBody
    @RequestMapping("getOpenPermission")
    public String getOpenPermission(@RequestParam Integer empId, @RequestParam Integer deviceId, @RequestParam String openType) {
        try {
            //  String menu = request.getParameter("menu");
            if (Common.isEmpty(openType)) {
                throw new Exception("缺少必要参数！");
            }
            boolean res = service.isOpen(empId, deviceId, openType);
            return LResult.Success(res).toJson();
        } catch (Exception ex) {
            return LResult.Error(ex.getMessage()).toJson();
        }
    }

}
